/**
 * 显示消息提示框
 * @param content 提示的标题
 */

import {
	isNull
} from "./validate";

let createUserNameList = []
let corolList = [

	{
		value: 2,
		label: "绿色",
		color: "#ecffe5",
	},
	{
		value: 4,
		label: "黄色",
		color: "#f5ebbc"
	},
	{
		value: 0,
		label: "红色",
		color: "#ffcccc"
	},

	{
		value: 1,
		label: "蓝色",
		color: "#c8e5ff"
	},
	{
		value: 5,
		label: "紫色",
		color: "#f5cbff",
	},
	{
		value: 3,
		label: "灰色",
		color: "#aba9a9"
	},

	{
		value: 9,
		label: "无色",
		color: "#fff"
	},
]




export function formattersysClassroomName(uid) {
	let createUserNameList = getcommon('sysClassroomList')
	for (var x = 0; x < createUserNameList.length; x++) {
		if (createUserNameList[x].id == uid) {
			return createUserNameList[x].name;
		}
	}
}

export function shijianduanaa(uid) {
	let createUserNameList = getcommon('createUserNameList')
	for (var x = 0; x < createUserNameList.length; x++) {
		if (createUserNameList[x].id == uid) {
			return (
				createUserNameList[x].startTime + "-" + createUserNameList[x].outTime
			);
		}
	}
}

export function getShiJianDuanById(uid) {
	let shijianduanList = getcommon('shijianduan')
	for (var x = 0; x < shijianduanList.length; x++) {
		if (shijianduanList[x].id == uid) {
			return (
				shijianduanList[x].startTime + "-" + shijianduanList[x].outTime
			);
		}
	}
}
let isShowLoading = false
let isShowToast = false
export function toast(content, icon = 'none') {
	isShowToast = true
	uni.showToast({
		icon: icon,
		title: content,
		duration: 1000,
	})

	// 定时器，1500 毫秒后执行
	setTimeout(() => {
		// 在这里写需要延迟执行的代码
		isShowToast = false
	}, 1000);

}


export function showLoading(content = '加载中') {

	if (isShowToast) {
		console.log("toast运行中");
		return
	}
	isShowLoading = true
	uni.showLoading({
		title: content
	})


}
export function hideLoading() {
	isShowLoading = false
	uni.hideLoading()
}



export function getColorByCode(code) {
	if (code == null) {
		return '#fff'
	}
	for (let s of corolList) {
		if (s.code == code) {
			return s.color
		}
	}
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content, isOk = true) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			showCancel: isOk,

			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}

/**
 * 头像处理方法 data 是学生
 * @param {Object} data
 */
export function pictureProcessing(data) {

	if (data == null) {
		return "https://api3.m.grasswave.cn/opsli-boot/resources/static/files/img/boy.png";
	}
	if (data.sex == 1 && (data.imageUrl == null || data.imageUrl == "")) {
		return "https://api3.m.grasswave.cn/opsli-boot/resources/static/files/img/boy.png";
	}

	if ((data.sex == 0 && data.imageUrl == null) || data.imageUrl == "") {
		return "https://api3.m.grasswave.cn/opsli-boot/resources/static/files/img/girl.png";
	}

	return data.imageUrl;
}


export function formatterUserName(uid) {

	let asdf = getcommon('createUserNameList')

	for (var x = 0; x < asdf.length; x++) {
		if (asdf[x].id == uid) {
			return asdf[x].realName;
		}
	}
}


/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 获取common 中的常用数
 * @param commonFind 字段
 * @returns {void|*}
 */
export function getcommon(commonFind) {
	try {
		if (commonFind == null) {
			return [];
		}
		let asdf = JSON.parse(localStorage.getItem("common"))[commonFind]
		return asdf;
	} catch (e) {
		return []
	}

}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 存储common 中的常用数
 * @param commonFind 字段
 * @param data 数据
 * @returns {void|*}
 */
export function setcommon(commonFind, data) {


	try {

		let common = JSON.parse(localStorage.getItem("common"))

		if (isNull(common)) {
			common = {}
		}

		common[commonFind] = data

		localStorage.setItem("common", JSON.stringify(common));
	} catch (e) {
		return null
	}

}



/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 存储common 中的常用数
 * @param commonFind 字段
 * @param data 数据
 * @returns {void|*}
 */
export function clear() {
	console.log("清除缓存");
	localStorage.removeItem("common")

}